<?php
namespace app\api\controller\v1;

use app\api\model\Order as Orders;
use think\Request;

class Order extends Base
{

    public function index(Orders $order,Request $request,$id){

        $token = $request->header('token');

        $order=  $order->getOrder($id,$token);

        return $this->success($order);
    }

    public function store(Orders $order,Request $request)
    {
        $token = $request->header('token');

        $order = $order->createOrder($token);

        if ($order){

            return $this->success($order);

        }

        return $this->error();
    }

    public function confirmOrder(Orders $order,Request $request)
    {
        $id = $request->param('id');
        $address_id = $request->param('address_id');

        if (empty($address_id)){
            return $this->error('请先添加默认地址');
        }


        $order = $order->confirm($id,$address_id);

        if ($order){


            return $this->success();
        }

        return $this->error();

        
    }


    public function getAllOrders(Orders $order,Request $request)
    {
        $token = $request->header('token');

        $order = $order->lists($token);

        if ($order){

            return $this->success($order);
        }

        return $this->error();

    }

    public function takeGoods(Orders $order,$id)
    {

        $order = $order->take($id);

        if ($order){


            return $this->success();
        }

        return $this->error();


    }

    public function cancelOrder(Orders $order,$id)
    {
        $order = $order->cancel($id);

        if ($order){

            return $this->success();
        }

        return $this->error();

    }
}
